package com.ruoyi.web.pojo.request.device;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.web.domain.device.DeviceInfo;
import com.ruoyi.web.utils.BasePageQuery;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class DeviceInfoRequest extends BasePageQuery {

    @ApiModelProperty("设备编号")
    private String deviceId;

    @ApiModelProperty("设备类型 多个用逗号隔开")
    private String deviceType;

    @ApiModelProperty(value = "在线状态: offline:离线, online:在线, fault:故障 ,nothing:无")
    private String lineState;

    @ApiModelProperty("设备类型")
    private String deviceName;

    @ApiModelProperty("产品id 多个用,号隔开")
    private String productId;

    @ApiModelProperty("设备类型List - 不传递")
    private List<String> deviceTypes = new ArrayList<>();

    @ApiModelProperty("设备类型List - 不传递")
    private List<Long> productIdList = new ArrayList<>();

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
        if(deviceType != null){
            this.deviceTypes = CollUtil.newArrayList(deviceType.split(","));
        }
    }

    public void setProductId(String productId) {
        this.productId = productId;
        if(productId != null){
            List<Long> ids = Arrays.stream(productId.split(","))
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            this.productIdList = ids;
        }
    }

    /**
     * @desc  构造查询条件
     * @author fwx 2024/1/29 17:18
     **/
    public void bulidWrapper(QueryWrapper<DeviceInfo> query){
        //设备编号
        if(StringUtils.isNotBlank(this.getDeviceId())){
            query.lambda().like(DeviceInfo::getDeviceId,this.getDeviceId());
        }

        //设备名称
        if(StringUtils.isNotBlank(this.getDeviceName())){
            query.lambda().like(DeviceInfo::getDeviceName,this.getDeviceName());
        }
        
        //在线离线
        if(StringUtils.isNotBlank(this.getLineState())){
            query.lambda().eq(DeviceInfo::getLineState,this.getLineState());
        }
        
        //设备类型
        if(StringUtils.isNotBlank(this.getDeviceType())){
            query.lambda().in(DeviceInfo::getDeviceType,this.getDeviceTypes());
        }

        //产品id
        if(StringUtils.isNotBlank(this.getProductId())){
            query.lambda().in(DeviceInfo::getProductId,this.getProductIdList());
        }

    }

}
